package com.ruoyi.linxi.business;

import com.ruoyi.common.utils.number.BigDecimalUtil;
import com.ruoyi.linxi.entry.StandingBook;
import com.ruoyi.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title TODO
 * @Description TODO
 * @Author CAI ZHAI
 * @Date 2021/10/1 8:58
 **/
@Slf4j
public class StandingBookUtil{
    
    public static void exportStandingBook(List<StandingBook> standingBookList){
        
        for(StandingBook standingBook : standingBookList){
            BigDecimal qfje = BigDecimalUtil.stringToBigDecimal(standingBook.getQfje());
            if(qfje==null){
                standingBook.setSfCx(0);
                continue;
            }
            
            if(qfje.compareTo(new BigDecimal(-1))<0 && qfje.compareTo(new BigDecimal(1))>0){
                if(qfje.compareTo(new BigDecimal(0))<0){ //预交费
                
                }else { //欠费

                
                }
                
            }else {
                //结清
                
            }
        }
    }
    
    
    public static List<StandingBook> sheetToObj(String fileName){
        List<StandingBook> resList = new ArrayList<>();
        try{
            InputStream is = new FileInputStream(new File(fileName));
            Workbook hssfWorkbook = null;
            if (fileName.endsWith("xlsx")) {
                hssfWorkbook = new XSSFWorkbook(is);// Excel 2007zz
            } else if (fileName.endsWith("xls")) {
                hssfWorkbook = new HSSFWorkbook(is);// Excel 2003
            
            }
            // 循环工作表Sheet
            // HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            Sheet hssfSheet = hssfWorkbook.getSheetAt(0);
            if(hssfSheet==null){
                log.error("sheet==null");
                return null;
            }
        
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row row = hssfSheet.getRow(rowNum);
                if(row!=null){
                    StandingBook standingBook = new StandingBook();
                    standingBook.setFh(ExcelUtil.getStringValue(row, 0));
                    standingBook.setBh(ExcelUtil.getStringValue(row, 1));
                    standingBook.setYzxm(ExcelUtil.getStringValue(row, 2));
                    standingBook.setWyf(ExcelUtil.getStringValue(row, 4));
                    standingBook.setLjf(ExcelUtil.getStringValue(row, 5));
                    standingBook.setDf(ExcelUtil.getStringValue(row, 6));
                    standingBook.setXj(ExcelUtil.getStringValue(row, 7));
                    standingBook.setLsqfmx(ExcelUtil.getStringValue(row, 8));
                    standingBook.setQfje(ExcelUtil.getStringValue(row, 23));
                    standingBook.setSfCx(1);
                    resList.add(standingBook);
                }
            }
        
        }catch(Exception e){
            e.printStackTrace();
        }
    
        return resList;
    }
}
